package com.serkol.openai;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.openai.api.OpenAiApi;

import java.io.IOException;

// 方案一：自定义枚举反序列化逻辑（推荐）
public class RoleDeserializer extends StdDeserializer<OpenAiApi.ChatCompletionMessage.Role> {
    public RoleDeserializer() {
        super(OpenAiApi.ChatCompletionMessage.Role.class);
    }

    @Override
    public OpenAiApi.ChatCompletionMessage.Role deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException {
        String value = p.getValueAsString();
        if (StringUtils.isEmpty(value)) {
            return null; // 或返回默认角色 OpenAiApi.ChatCompletionMessage.Role.ASSISTANT
        }
        return OpenAiApi.ChatCompletionMessage.Role.valueOf(value);
    }
}